﻿using System.Collections.Generic;

namespace Telex;

sealed class ProtocolRegister : IProtocolRegister
{
    public ProtocolRegister(ProtocolMap map)
    {
        foreach (var item in map.sendProtocolMap)
            this.TryAddSendProtocol(item.Key, item.Value);
        foreach (var item in map.callProtocolMap)
            this.TryAddCallProtocol(item.Key, item.Value);
    }
    
    private Dictionary<uint, SendProtocolDelegate> sendProtocolMap = new();
    private Dictionary<uint, CallProtocolDelegate> callProtocolMap = new();
    
    public bool TryAddSendProtocol(uint protocolId, SendProtocolDelegate func)
    {
        return this.sendProtocolMap.TryAdd(protocolId, func);
    }
    
    public bool TryAddCallProtocol(uint protocolId, CallProtocolDelegate func)
    {
        return this.callProtocolMap.TryAdd(protocolId, func);
    }

    public bool TryGetSendProtocolFunc(uint protocolId, out SendProtocolDelegate func)
    {
        return this.sendProtocolMap.TryGetValue(protocolId, out func);
    }

    public bool TryGetCallProtocolFunc(uint protocolId, out CallProtocolDelegate func)
    {
        return this.callProtocolMap.TryGetValue(protocolId, out func);
    }
}